package com.wuyangnju.healthclub.service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wuyangnju.healthclub.dao.BankcardItemDao;
import com.wuyangnju.healthclub.dao.FamilyDao;
import com.wuyangnju.healthclub.dao.SingleDao;
import com.wuyangnju.healthclub.entity.BankcardItem;
import com.wuyangnju.healthclub.entity.Family;
import com.wuyangnju.healthclub.entity.Single;
import com.wuyangnju.healthclub.util.VirDate;

@Component
public class FinancialService {
	@PersistenceContext
	protected EntityManager em;

	@Resource
	private SingleDao singlePersonDao;

	@Resource
	private FamilyDao familyDao;

	@Resource
	private BankcardItemDao bankcardItemDao;

	@Transactional
	public void singleCharge(String cardId, double charge) {
		Single singlePerson = singlePersonDao
				.retrieve(em, Long.valueOf(cardId));
		singlePerson.setBalance(singlePerson.getBalance() + charge);
		singlePersonDao.update(em, singlePerson.getId(), singlePerson);
		BankcardItem bankcardItem = new BankcardItem(VirDate.get().getDate(),
				singlePerson, charge);
		bankcardItemDao.create(em, bankcardItem);
	}

	@Transactional
	public void singlePay(String cardId) {
		Single singlePerson = singlePersonDao
				.retrieve(em, Long.valueOf(cardId));
		if (singlePerson.getBalance() >= singlePerson.getMonthlyPay()) {
			singlePerson.setBalance(singlePerson.getBalance()
					- singlePerson.getMonthlyPay());
			singlePersonDao.update(em, singlePerson.getId(), singlePerson);
			BankcardItem bankcardItem = new BankcardItem(VirDate.get()
					.getDate(), singlePerson, -singlePerson.getMonthlyPay());
			bankcardItemDao.create(em, bankcardItem);
		} else {
			singlePerson.setMonthDelay(singlePerson.getMonthDelay() + 1);
			singlePersonDao.update(em, singlePerson.getId(), singlePerson);
		}
	}

	@Transactional
	public void familyCharge(String cardId, double charge) {
		Family family = familyDao.retrieve(em, Long.valueOf(cardId));
		family.setBalance(family.getBalance() + charge);
		familyDao.update(em, family.getId(), family);
		BankcardItem bankcardItem = new BankcardItem(VirDate.get().getDate(),
				family, charge);
		bankcardItemDao.create(em, bankcardItem);
	}

	@Transactional
	public void familyPay(String cardId) {
		Family family = familyDao.retrieve(em, Long.valueOf(cardId));
		if (family.getBalance() >= family.getMonthlyPay()) {
			family.setBalance(family.getBalance() - family.getMonthlyPay());
			familyDao.update(em, family.getId(), family);
			BankcardItem bankcardItem = new BankcardItem(VirDate.get()
					.getDate(), family, -family.getMonthlyPay());
			bankcardItemDao.create(em, bankcardItem);
		} else {
			family.setMonthDelay(family.getMonthDelay() + 1);
			familyDao.update(em, family.getId(), family);
		}
	}
}